package com.zany.children.action;

import com.ibatis.sqlmap.engine.type.BaseTypeHandler;
import com.ibatis.sqlmap.engine.type.TypeHandler;

import java.sql.*;
import java.util.Date;

public class TypeHandlerCallback extends BaseTypeHandler implements
        TypeHandler {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Object o, String s) throws SQLException {
        preparedStatement.setObject(i, o);
    }

    public Object getResult(ResultSet rs, String columnName)
            throws SQLException {
        Object object = rs.getObject(columnName);
        if (rs.wasNull()) {
            return null;
        } else {
            boolean b = object instanceof java.sql.Date;
            if (b) {
                object = new Date(rs.getTimestamp(columnName).getTime());
            }
            return object;
        }
    }

    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        Object object = rs.getObject(columnIndex);
        if (rs.wasNull()) {
            return null;
        } else {
            boolean b = object instanceof java.sql.Date;
            if (b)
                object = new Date(rs.getTimestamp(columnIndex).getTime());
            return object;
        }
    }

    public Object getResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        Object object = cs.getObject(columnIndex);
        if (cs.wasNull()) {
            return null;
        } else {
            boolean b = object instanceof java.sql.Date;
            if (b)
                object = new Date(cs.getTimestamp(columnIndex).getTime());
            return object;
        }
    }

    public Object valueOf(String s) {
        return s;
    }

//    public static String dateToStr(java.sql.Timestamp time, String strFormat) {
//            DateFormat df = new SimpleDateFormat(strFormat);
//            String str = df.format(time);
//            return str;
//         }
//
//    public static java.sql.Timestamp strToSqlDate(String strDate, String dateFormat) {
//        SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
//        java.util.Date date = null;
//        try {
//            date = sf.parse(strDate);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        java.sql.Timestamp dateSQL = new java.sql.Timestamp(date.getTime());
//        return dateSQL;
//    }
//
    public static void main(String[] args) {
//        String strDate = dateToStr(date, "yyyy-MM-dd HH:mm:ss SSS");
//        return strToSqlDate(strDate, "yyyy-MM-dd HH:mm:ss SSS");
//        java.util.Date tspToDate = new java.util.Date(new java.sql.Timestamp(new Date().getTime()));
//        System.out.println(tspToDate);

    }

}
